package com.lc.cehui.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.fragment.HomeFragment;
import com.lc.cehui.ui.fragment.MeFragment;
import com.lc.cehui.ui.fragment.MeassageFragment;
import com.lc.cehui.ui.fragment.ReleaseFragment;
import com.lc.cehui.ui.fragment.ShopFragment;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import butterknife.ButterKnife;

public class MainActivity extends AppActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    Fragment fragment = null;
    RadioGroup rgHomePageBottom;
    private RadioButton radioButton1, radioButton2, radioButton3, radioButton4;
    private ImageButton imageButton;
    private List<Fragment> mFragmentList;
    private FragmentTransaction mFragmentTransaction;
    private RadioButton radioButton5;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        mFragmentList = new ArrayList<>();
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new MeassageFragment());
        mFragmentList.add(new ReleaseFragment());
        mFragmentList.add(new ShopFragment());
        mFragmentList.add(new MeFragment());
        radioButton1 = findViewById(R.id.rb_home);
        radioButton2 = findViewById(R.id.rb_message);
        radioButton3 = findViewById(R.id.rb_shop);
        radioButton4 = findViewById(R.id.rb_my);
        radioButton5 = findViewById(R.id.rb_release);

        rgHomePageBottom = findViewById(R.id.rg_home_page_bottom);
        rgHomePageBottom.setOnCheckedChangeListener(this);
        switchContent(mFragmentList.get(0));

        if (SPUtil.get(getContext(), IntentKey.RID, 0)==1) {
            radioButton5.setVisibility(View.VISIBLE);
        }else {
            radioButton5.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            return false;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                switchContent(mFragmentList.get(0));
                break;
            case R.id.rb_message:
                switchContent(mFragmentList.get(1));
                break;
            case R.id.rb_release:
                switchContent(mFragmentList.get(2));
                break;
            case R.id.rb_shop:
                switchContent(mFragmentList.get(3));
                break;
            case R.id.rb_my:
                switchContent(mFragmentList.get(4));
                break;
        }
    }

    private Fragment mContent = new Fragment();

    private void switchContent(Fragment to) {
        Log.e("addFragment", "### " + to.getId() + "  " + to.isAdded() + "  " + to.isHidden());
        if (mContent != to) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (!to.isAdded()) {
                transaction.hide(mContent).add(R.id.fg_main, to).commitAllowingStateLoss();
            } else {
                transaction.hide(mContent).show(to).commitAllowingStateLoss();
            }
            mContent = to;
        }
    }
}